<?php
/* SVN FILE: $Id: item_musicplayer.php 247 2009-10-22 15:31:39Z supersmashbrothers $ */
/**
* ibMarket 2
* IP.Board 3.0
* Profile Song item
*
* @author $Author: supersmashbrothers $
* @copyright $Copyright$
* @version $Revision: 247 $
* @lastrevision $Date: 2009-10-22 15:31:39 +0000 (Thu, 22 Oct 2009) $
* @modifiedby $LastChangedBy: supersmashbrothers $
* @lastmodified $LastChangedDate: 2009-10-22 15:31:39 +0000 (Thu, 22 Oct 2009) $
* @filesource $URL: http://ipb-ibmarket.googlecode.com/svn/ibmarket2/trunk/admin/applications_addon/other/ibmarket/items/own/item_musicplayer.php $
*/

class item_musicplayer extends templateItem
{
	/**
	 * The default name of the item
	 *
	 * @access	protected
	 * @var		string
	 */
	protected $defaultName = "Add a Song";
	
	/**
	 * The default description of the item
	 *
	 * @access	protected
	 * @var		string
	 */
	protected $defaultDescription = "Add a Song to your profile";
	
	// Inherited Properties
	/**
	 * Item setting to allow multiple instances within ibMarket 
	 *
	 * @access	protected
	 * @var		boolean
	 */
	protected $allowMultiple = true;
	
	/**
	 * Item specific setting
	 *
	 * @access	protected
	 * @var		array
	 */
	protected $itemSettings = array();
	
	/**
	 * Constructor to setup the item
	 *
	 * @access	public
	 * @param	ipsRegistry	$registry	IPB registry
	 * @param	array		&$item		The item information
	 */
	public function __construct(ipsRegistry $registry, array &$item)
	{
		parent::__construct($registry, $item);
		
		if($item['item_config'] || $item['item_id'] != 0)
		{
			$this->itemSettings = unserialize($item['item_config']);
		}
		else
		{
			$this->itemSettings['song'] = "";
			$this->itemSettings['upload'] = 1;
			$this->itemSettings['max_size'] = 5120;
		}
	}
	
	/**
	 * Additional settings when adding/editing the item
	 *
	 * @access	public
	 * @return	string|bool	string if there is something to edit, false otherwise
	 */
	public function adminForm()
	{
		$form['song'] = ipsRegistry::getClass('output')->formInput('item[song]', $this->itemSettings['song']);
		$form['upload'] = ipsRegistry::getClass('output')->formYesNo('item[upload]', $this->itemSettings['upload']);
		$form['max_size'] = ipsRegistry::getClass('output')->formSimpleInput('item[max_size]', $this->itemSettings['max_size']);
		return <<<EOF
				<li>
					<label>
						{$this->lang->words['song_to_add']}
						<span class="desctext">{$this->lang->words['song_to_add_desc']}</span>
					</label>
					{$form['song']}
				</li>
				<li>
					<label>{$this->lang->words['upload_song']}</label>
					{$form['upload']}
				</li>
				<li>
					<label>
						{$this->lang->words['upload_song_max_size']}
						<span class="desctext">{$this->lang->words['upload_song_max_size_desc']}</span>
					</label>
					{$form['max_size']} KiB
				</li>

EOF;
	}
	
	/**
	 * Saving to adding/editing the item
	 *
	 * @access	public
	 * @param	array	&$item_info		The new item information to be stored in the database.
	 * @param	array	[$input]		Relavent input relating to the item
	 * @todo
	 * @return	void
	 */
	public function adminSave(array &$item_info, array $input)
	{
		$upload_path = $this->settings['upload_dir'];
		if ( ! is_dir( $upload_path . "/profile_song" ) )
		{
			if ( @mkdir( $upload_path . "/profile_song", 0777 ) )
			{
				@chmod( $upload_path . "/profile_song", 0777 );
				
				# Set path and dir correct
				$upload_path .= "/profile_song";
				$upload_dir   = "profile_song/";
			}
			else
			{
				$upload_dir   = "";
			}
		}
		else
		{
			# Set path and dir correct
			$upload_path .= "/profile_song";
			$upload_dir   = "profile_song/";
		}
		
		$setting = array();
		$setting['song'] = $input['song'];
		$setting['upload'] = intval($input['upload']) & 1;
		$setting['max_size'] = max(intval($input['max_size']),0);
		$setting['song_loc'] = $upload_path;
		$setting['song_dir'] = $upload_dir;
		
		$item_info['item_config'] = serialize($setting);
		
		if($this->DB->checkForField("pp_song", "profile_portal"))
		{
			$this->DB->changeField("profile_portal","pp_song","pp_song","varchar(255) NOT NULL","''");
		}
		else
		{
			$this->DB->addField("profile_portal","pp_song","varchar(255) NOT NULL","''");
		}
	}
	
	/**
	 * When quick saving the price and stock
	 *
	 * @access	public
	 * @param	array	&$item_info		The new item information to be stored in the database.
	 * @param	string	$type			Possible values: 'stockprice', more possibly in the future
	 * @return	void
	 */
	public function adminQuickSave(array &$item_info, $type)
	{
		return;
	}
	
	/**
	 * When removing the item from ibMarket
	 *
	 * @access	public
	 * @return	void
	 */
	public function adminRemove()
	{
		if(count(marketRegistry::getItemsByFiles($this->item['filename'])) == 1)
		{
			$this->DB->dropField("profile_portal","pp_song");
		}
	}
	
	/**
	 * Viewing the file edits if any
	 *
	 * @access	public
	 * @return	string|bool	string if there is something to edit, false otherwise
	 */
	public function adminViewFileEdits()
	{
		return <<<EOF
<div id='skinEditsProfile'>
	<div class='acp-box'>
		<h3>
			{$this->lang->words['skin_template']}: {$this->lang->words['bit_skin_profile']}
		</h3>
		<table width='100%' cellpadding='0' cellspacing='0' border='0' class='alternate_rows'>
			<tr>
				<th colspan='2'>profileModern</th>
			</tr>
			<tr>
				<td style='white-space:nowrap;width:1px;min-width:1%'>
					{$this->lang->words['find']}
				</td>
				<td>
					<textarea style="width: 98%;" cols="80" rows="3">			<img class="photo" src='{\$member['pp_main_photo']}' alt="{\$member['members_display_name']}{\$this->lang->words['users_photo']}"  />
</textarea>
				</td>
			</tr>
			<tr>
				<td style='white-space:nowrap;width:1px;min-width:1%'>
					{$this->lang->words['add_after']}
				</td>
				<td>
					<textarea style="width: 98%;" cols="80" rows="10"><if test="\$member['pp_song']">
<div>
	<object type="application/x-shockwave-flash" data="{\$this->settings['board_url']}/public/mp3player.swf" width="150" height="15">
		 <param name="movie" value="{\$this->settings['board_url']}/public/mp3player.swf" />
		 <param name="FlashVars" value="mp3={\$member['pp_song']}&autoplay=1&loop=0&volume=100&showstop=1&showinfo=1" />
	</object>
</div>
</if>
</textarea>
				</td>
			</tr>
			<tr>
				<td style='white-space:nowrap;width:1px;min-width:1%'>
					{$this->lang->words['it_will_look_like']}
				</td>
				<td>
					<textarea style="width: 98%;" cols="80" rows="10">			<img class="photo" src='{\$member['pp_main_photo']}' alt="{\$member['members_display_name']}{\$this->lang->words['users_photo']}"  />
<if test="\$member['pp_song']">
<div>
	<object type="application/x-shockwave-flash" data="{\$this->settings['board_url']}/public/mp3player.swf" width="150" height="15">
		 <param name="movie" value="{\$this->settings['board_url']}/public/mp3player.swf" />
		 <param name="FlashVars" value="mp3={\$member['pp_song']}&autoplay=1&loop=0&volume=100&showstop=1&showinfo=1" />
	</object>
</div>
</if>
</textarea>
				</td>
			</tr>
		</table>
	</div>
</div>
<br />
<div id='skinEditsGlobal'>
	<div class='acp-box'>
		<h3>
			{$this->lang->words['skin_template']}: {$this->lang->words['bit_skin_global']}
		</h3>
		<table width='100%' cellpadding='0' cellspacing='0' border='0' class='alternate_rows'>
			<tr>
				<th colspan='2'>userInfoPane</th>
			</tr>
			<tr>
				<td style='white-space:nowrap;width:1px;min-width:1%'>
					{$this->lang->words['find']}
				</td>
				<td>
					<textarea style="width: 98%;" cols="80" rows="1">		<if test="membertitle:|:\$author['title']"></textarea>
				</td>
			</tr>
			<tr>
				<td style='white-space:nowrap;width:1px;min-width:1%'>
					{$this->lang->words['add_before']}
				</td>
				<td>
					<textarea style="width: 98%;" cols="80" rows="10"><if test="\$author['pp_song']">
<li style='text-align:center'>
	<object type="application/x-shockwave-flash" data="{\$this->settings['board_url']}/public/mp3player.swf" width="150" height="15">
		 <param name="movie" value="{\$this->settings['board_url']}/public/mp3player.swf" />
		 <param name="FlashVars" value="mp3={\$author['pp_song']}&autoplay=0&loop=0&volume=100&showstop=1&showinfo=1" />
	</object>
</li>
</if>
</textarea>
				</td>
			</tr>
			<tr>
				<td style='white-space:nowrap;width:1px;min-width:1%'>
					{$this->lang->words['it_will_look_like']}
				</td>
				<td>
					<textarea style="width: 98%;" cols="80" rows="10"><if test="\$author['pp_song']">
<li style='text-align:center'>
	<object type="application/x-shockwave-flash" data="{\$this->settings['board_url']}/public/mp3player.swf" width="150" height="15">
		 <param name="movie" value="{\$this->settings['board_url']}/public/mp3player.swf" />
		 <param name="FlashVars" value="mp3={\$author['pp_song']}&autoplay=0&loop=0&volume=100&showstop=1&showinfo=1" />
	</object>
</li>
</if>
		<if test="membertitle:|:\$author['title']"></textarea>
				</td>
			</tr>
		</table>
	</div>
</div>
EOF;
	}
	
	/**
	 * Any admin AJAX functionality
	 *
	 * @access	public
	 * @param	array	$invItem		The inventory item
	 * @return	void
	 */
	public function adminAjax()
	{
		return;
	}
	
	/**
	 * Check and modify the intentory item information before storing in the database
	 *
	 * @access	public
	 * @param	array	&$invItem		The new inventory item information to be stored in the database.
	 * @return	bool	true if the item is successfully created, false otherwise
	 */
	public function publicOnCreateItem(array &$invItem)
	{
		return true;
	}
	
	/**
	 * When using the item
	 * Show a form and validate the form
	 *
	 * @access	public
	 * @param	array	$invItem		The inventory item
	 * @return	string|bool				string if there is a form to be used, true if successfully used, false otherwise
	 */
	public function publicUseItem(array &$invItem)
	{
		$this->registry->getClass('class_localization')->loadLanguageFile( array( 'public_items' ) );
		
		if($this->itemSettings['song'] || $this->request['request_method'] == 'post')
		{
			$song = "";
			
			// Remove Song
			if(file_exists($this->itemSettings['song_loc']."/song-".$this->memberData['member_id'].".mp3"))
			{
				@unlink($this->itemSettings['song_loc']."/song-".$this->memberData['member_id'].".mp3");
			}
			if($this->settings['upload_dir'] != $this->itemSettings['song_loc'] && file_exists($this->settings['upload_dir']."/song-".$this->memberData['member_id'].".mp3"))
			{
				@unlink($this->settings['upload_dir']."/song-".$this->memberData['member_id'].".mp3");
			}
			
			if($this->itemSettings['song'] != "" || $this->itemSettings['song'] == "{remove}")
			{
				$song = $this->itemSettings['song'] == "{remove}" ? "" : $this->itemSettings['song'];
			}
			elseif($this->itemSettings['upload'] && isset($_FILES['upload_song']) && is_uploaded_file($_FILES['upload_song']['tmp_name']))
			{
				require_once(IPS_KERNEL_PATH."classUpload.php");
				$upload = new classUpload();
				
				$upload->out_file_name = 'song-'.$this->memberData['member_id'];
				$upload->out_file_dir = $this->itemSettings['song_loc'];
				$upload->max_file_size = $this->itemSettings['max_size'] * 1024;
				$upload->allowed_file_ext = array('mp3');
				$upload->upload_form_field = 'upload_song';
				
				$upload->process();
				
				if($upload->error_no)
				{
					IPSMember::save( $this->memberData['member_id'], array( 'extendedProfile' => array( 'pp_song' => "" ) ) );
					
					$this->registry->getClass('output')->showError('upload_failed');
				}
				
				$song = $this->settings['upload_url']."/".$this->itemSettings['song_dir'].$upload->parsed_file_name;
			}
			else
			{
				$song = $this->request['song_url'];
			}
			
			IPSMember::save( $this->memberData['member_id'], array( 'extendedProfile' => array( 'pp_song' => $song ) ) );
			
			$this->deleteItems($invItem);
			
			$this->writeLog($invItem, "Added song to profile");
			
			$this->registry->getClass('output')->redirectScreen( $this->lang->words['song_profile_made'], $this->settings['base_url_with_app'] . "module=inventory" );
		}
		
		$html = <<<EOF
<form id='addSongForm' enctype="multipart/form-data" method="post" action="{$this->settings['base_url_with_app']}module=inventory&amp;section=item&amp;item={$invItem['i_id']}">
	<div id='addSong'class='block_wrap'>
		<h3 id='addSongHeader' class='maintitle'>{$this->item['item_name']}</h3>
		<fieldset class="row2">
			<ul>
				<li>
					<label>{$this->lang->words['pick_song_url']}</label>
					<input type='text' name='song_url' value='' class='input_text' />
				</li>

EOF;
		if($this->itemSettings['upload'])
		{
			$html .= <<<EOF
				<li>
					<label>{$this->lang->words['upload_a_song']}</label>
					<input type="file" size="30" name="upload_song" id="upload_song" />
				</li>

EOF;
		}
		$html .= <<<EOF
			</ul>
		</fieldset>
		<fieldset class="submit">
			<input class="input_submit" type="submit" value="{$this->lang->words['add_song']}" name="submit" />
		</fieldset>
	</div>
</form>

EOF;
		return $html;
	}
	
	/**
	 * Any AJAX functionality
	 *
	 * @access	public
	 * @param	array	$invItem		The inventory item
	 * @return	void
	 */
	public function publicAjax(array &$invItem)
	{
		return;
	}
	
	/**
	 * When the item is being sold back
	 *
	 * @access	public
	 * @param	array	&$invItem	The inventory item information
	 * @return	bool				true if successful, false otherwise
	 */
	public function publicOnResoldItem(array &$invItem)
	{
		return true;
	}
	
	/**
	 * When the item is being restocked
	 *
	 * @access	public
	 * @param	array	&$item	The inventory item information
	 * @return	void
	 */
	public function onItemRestock(array &$item)
	{
		return;
	}
}
